import 'package:flutter/material.dart';

///
/// des:
///

class AnimatedBuilderDemo extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _AnimatedBuilderDemo();
}

class _AnimatedBuilderDemo extends State<AnimatedBuilderDemo> with SingleTickerProviderStateMixin {
  AnimationController animationController;
  var animation;

  @override
  void initState() {
    super.initState();
    animationController = AnimationController(
      duration: Duration(seconds: 1),
      // vsync: this // vsync: this, // del by wcx TODO
    )..addStatusListener((status) {
        if (status == AnimationStatus.completed) {
          //执行结束反向执行
          animationController.reverse();
        } else if (status == AnimationStatus.dismissed) {
          //反向执行结束正向执行
          animationController.forward();
        }
      });

    animation = CurvedAnimation(parent: animationController, curve: Curves.easeIn); // 给控制器 添加 曲线
    animation = Tween(begin: 80.0, end: 100.0).animate(animation); // 创建一个Tween 动画，设置开始值和结束值
    animationController.forward();
  }

  /// 构建UI
  @override
  Widget build(BuildContext context) {
    return Center(
      child: _AnimatedBuilder( // 使用自定义的AnimatedBuilder 组件
        child: FlutterLogo(),
        animation: animation,
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
    animationController.dispose();
  }
}

/// 自定义一个 AnimatedBuilder 组件
class _AnimatedBuilder extends StatelessWidget {
  _AnimatedBuilder({this.child, this.animation});

  final Animation<double> animation; // 动画, Animation<double> 的子类
  final Widget child;

  /// 构建UI
  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: animation,
      builder: (context, child) {
        return Container(
          height: animation.value,
          width: animation.value,
          child: child,
        );
      },
      child: child,
    );
  }
}
